package zhujie2;

import java.lang.reflect.Method;

public class LoggableAnnotationTest {

    public static void main(String[] args) {
        LoggableService service = new LoggableService();

        // 获取 LoggableService 类的所有方法
        Method[] methods = LoggableService.class.getDeclaredMethods();

        for (Method method : methods) {
            // 检查方法上是否有 Loggable 注解
            if (method.isAnnotationPresent(Loggable.class)) {
                // 获取注解实例
                Loggable loggable = method.getAnnotation(Loggable.class);

                // 输出注解的值
                System.out.println("Method: " + method.getName() + " is loggable at level: " + loggable.level());

                // 模拟方法执行并记录日志
                try {
                    method.invoke(service);
                    // 这里可以添加实际的日志记录代码，例如使用 SLF4J 或 Log4j
                    System.out.println("Logging: " + method.getName() + " executed.");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                // 如果没有 Loggable 注解，则直接执行方法
                try {
                    method.invoke(service);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}